package lockOb;

public class Test {
    private boolean flag = true;
    public static void main(String[] args) {
        Test vt = new Test();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    vt.printA();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    vt.printB();
                }
            }
        }).start();
    }

    private synchronized void printA(){
        while (!flag){
            try{
                wait();
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        }
        System.out.print("A");
        flag = false;
        notify();
    }
    private synchronized void printB(){
        while (flag){
            try{
                wait();
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        }
        System.out.print("B");
        flag = true;
        notify();
    }

}
